/**
 * Created by George on 2018/2/25.
 */

import util from "../common/util"

// 将本控制器函数作为结果返回给router.js
function createActivityCtrl($scope, $http, $state) {
    $scope.welcome_info = "活动管理";
    $scope.$emit('updateRoute', 'activity');

    function init() {
        initOrUpdateData();
    }

    function initOrUpdateData() {
        $scope.activity = {
            name: "篮球活动",
            date: util.getNowFormatDate(),
            dues: 0,
            userNum: 0,
            info: "租场费、水费",
            selectUsers: [],
            usersData: [],
            type: "0",
            typeData: [
                {id: "0", name: "支出"},
                {id: "1", name: "收入"}
            ],
            number: 0
        };
        $scope.pageInfo = {
            pageNo: 1,
            pageSize: 100
        };

        $http.get("/basketballfire/rest/user/list", {params: $scope.pageInfo}).then(function (data, status) {
            console.log(data);
            var usersData = data.data.data;
            usersData.sort(function(a, b){return a.name.localeCompare(b.name)});
            initUsersData(usersData);
        });
    }

    function initUsersData(usersData) {
        $scope.activity.usersData = [];
        for (var index = 0; index < usersData.length; index++) {
            var user1 = {
                "id": usersData[index].id,
                "name": usersData[index].name,
                "checked": false,
                "number": 1
            };
            $scope.activity.usersData.push(user1);
        }
    }

    $scope.selectUserChange = function () {
        var number = 0;
        $scope.activity.selectUsers = [];
        for (var index = 0; index < $scope.activity.usersData.length; index++) {
            if ($scope.activity.usersData[index].checked === true && $scope.activity.usersData[index].number > 0) {
                var user = {
                    userId: $scope.activity.usersData[index].id,
                    userNum: $scope.activity.usersData[index].number,
                    name: $scope.activity.usersData[index].name
                };
                number += $scope.activity.usersData[index].number;
                $scope.activity.selectUsers.push(user);
            }
        }
        $scope.activity.userNum = number;
    };

    $scope.addActivityClick = function () {
        $scope.activity.selectUsers = [];
        var number = 0;
        for (var index = 0; index < $scope.activity.usersData.length; index++) {
            if ($scope.activity.usersData[index].checked === true && $scope.activity.usersData[index].number > 0) {
                var user = {
                    userId: $scope.activity.usersData[index].id,
                    userNum: $scope.activity.usersData[index].number,
                    name: $scope.activity.usersData[index].name
                };
                number += $scope.activity.usersData[index].number;
                $scope.activity.selectUsers.push(user);
            }
        }

        $scope.activity.userNum = number;
        if ($scope.activity.selectUsers.length <= 0) {
            console.log("must select ");
            alert("必须选择人员");
            return;
        }

        if ($scope.activity.type === "1" && $scope.activity.dues < 0 || $scope.activity.type === "0" && $scope.activity.dues > 0) {
            $scope.activity.dues = -$scope.activity.dues;
        }

        var data = {
            "name": $scope.activity.name,
            "type": $scope.activity.type,
            "date": $scope.activity.date,
            "dues": $scope.activity.dues,
            "userNum": $scope.activity.userNum,
            "records": $scope.activity.selectUsers,
            "info": $scope.activity.info
        };
        $http.post('/basketballfire/rest/activity', data).then(function () {
            console.log("success!");
            alert("创建成功！");
            $state.go("activity");
        }, function () {
            alert("创建失败！");
            console.log("failed!");
        });
    };

    //app start
    init();
}

export default createActivityCtrl;